﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace ArbolandiaBL
{
    public class Security
    {
        public Security()
        {
            //
            // TODO: Agregar aquí la lógica del constructor
            //
        }

        public static bool ValidLogin()
        {
            var res = false;
            try
            {

                var session = (ObSession)HttpContext.Current.Session["ObSession"];
                if (session != null)
                {
                    if (session.IdProfile > 0 && session.IdUser > 0
                        && session.PasswordCrypt != "" && session.ProfileType != ProfileType.Ninguno)
                    {
                        res = true;
                    }
                }

            }
            catch (Exception)
            {

                throw;
            }
            return res;
        }

        public static bool FirstLogin(string urs, string passw)
        {
            var res = false;
            var db = new ArbolandiaDBDataContext();
            var query = db.SystemUsers.Where(w => w.Username == urs);
            if (query.Count() > 0)
            {
                var passDecrypt = CryptoProvider.Decrypt(query.First().Password);
                if (passw.Equals(passDecrypt))
                {
                    res = true;
                    query.First().LastLoginDate = DateTime.Now;
                    LoadSession(query.First().Id, passw, (int)query.First().SystemProfileId, query.First().Username);
                    db.SubmitChanges();
                }
            }

            return res;

        }
        public static void LoadSession(int usr, string passw, Int32 profileId, string username)
        {
            var dbLoads = new ArbolandiaDBDataContext();
            ProfileType pt = ProfileType.Ninguno;
            var query = dbLoads.SystemProfiles.Where(w => w.Id == profileId);
            if (query.Count() > 0)
            {
                query.First().AccessAll = query.First().AccessAll == null ? false : query.First().AccessAll;
                query.First().AccessOrders = query.First().AccessOrders == null ? false : query.First().AccessOrders;
                query.First().AccessWareHouse = query.First().AccessWareHouse == null ? false : query.First().AccessWareHouse;
                if ((bool)query.First().AccessAll) { pt = ProfileType.Admin; }
                else if ((bool)query.First().AccessWareHouse) { pt = ProfileType.Almacen; }
                else if ((bool)query.First().AccessOrders) { pt = ProfileType.Pedidos; }
                else { pt = ProfileType.Ninguno; }

            }
            var session = new ObSession();
            session.IdProfile = profileId;
            session.IdUser = usr;
            session.PasswordCrypt = CryptoProvider.Encrypt(passw);
            session.UserName = username;
            session.ProfileType = pt;
            HttpContext.Current.Session["ObSession"] = session;
        }

        public static void LoggOff()
        {
            HttpContext.Current.Session.Abandon();
            HttpContext.Current.Response.Redirect(HttpContext.Current.Server.MapPath("~/Admin/Login.aspx"), false);
        }
        public static void SaveTestLogin(string usr, string password, ProfileType pt, string email)
        {

            var db = new ArbolandiaDBDataContext();
            db.SystemUsers.InsertOnSubmit(new SystemUsers()
            {
                Active = true,
                CreatedOn = DateTime.Now,
                Deleted = false,
                Email = email,
                IsAdmin = true,
                Password = CryptoProvider.Encrypt(password),
                SystemProfileId = (int?)pt,
                UserGuid = Guid.NewGuid(),
                Username = usr
            });
            db.SubmitChanges();
        }
    }
}
